package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.model.system.SysMovie;
import com.gec.model.vo.PageVo;
import com.gec.model.vo.PlayAuthVo;
import com.gec.model.vo.SysMovieQueryVo;
import com.gec.model.vo.SysMovieVo;
import com.gec.system.common.Result;
import com.gec.system.exception.MyCustomerException;
import com.gec.system.mapper.SysMovieMapper;
import com.gec.system.service.SysCategoryService;
import com.gec.system.service.SysMovieService;
import com.gec.system.util.BeanCopyUtils;
import com.gec.system.util.VodTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMovieServiceImpl extends ServiceImpl<SysMovieMapper, SysMovie> implements SysMovieService {

    @Autowired
    private SysMovieMapper sysMovieMapper;
    @Autowired
    private VodTemplate vodTemplate;
    @Autowired
    private SysCategoryService sysCategoryService;

    @Override
    public IPage<SysMovie> selectPage(IPage<SysMovie> p1, SysMovieQueryVo sysMovieQueryVo) {
        return this.baseMapper.selectPage(p1,sysMovieQueryVo);
    }

    @Override
    public Result getMoviePage(Integer pageNum, Integer pageSize, String name, int cid) {
        //根据名字查询影片(动态 sql)
        LambdaQueryWrapper<SysMovie> queryWrapper = new LambdaQueryWrapper<>();
        //模糊匹配影片名
        queryWrapper.like(StringUtils.hasText(name), SysMovie::getName, name);
        //匹配分类id
        queryWrapper.eq(cid != 0, SysMovie::getCid, cid);
        //分页查询
        Page<SysMovie> moviePage = new Page<>(pageNum, pageSize);
        page(moviePage, queryWrapper);

        //转换成 vo
        List<SysMovie> movies = moviePage.getRecords();
        List<SysMovieVo> movieVos = BeanCopyUtils.copyBeanList(movies, SysMovieVo.class);
        //设置分类名
        movieVos = movieVos.stream()
                .map(sysMovieVo -> sysMovieVo.setCategory(
                        sysCategoryService.getById(sysMovieVo.getCid()).getName()))
                .collect(Collectors.toList());

        //封装 pageVo
        PageVo pageVo = new PageVo(movieVos, moviePage.getTotal());
        return Result.ok(pageVo);
    }


    @Override
    public Result getNewMovieList(Integer pageNum, Integer pageSize, Long cid) {
        //根据分类 id查询影片(动态 sql)
        LambdaQueryWrapper<SysMovie> queryWrapper = new LambdaQueryWrapper<>();
        //匹配分类 id
        queryWrapper.eq(cid != 0L, SysMovie::getCid, cid);
        //根据创建时间倒序
        queryWrapper.orderByDesc(SysMovie::getCreateTime);
        //分页查询
        Page<SysMovie> moviePage = new Page<>(pageNum, pageSize);
        page(moviePage, queryWrapper);
        //封装 vo
        PageVo pageVo = new PageVo(moviePage.getRecords(), moviePage.getTotal());
        return Result.ok(pageVo);
    }

    @Override
    public Result getPlayAuthById(Long id) {
        //获取影片信息
        SysMovie movie = getById(id);
        //获取影片信息
        String image = movie.getImage();
        String playId = movie.getPlayId();
        String playAuth = null;
        try {
            //从阿里云查询视频播放密钥
            playAuth = vodTemplate.getVideoPlayAuth(playId).getPlayAuth();
        } catch (Exception e) {
            throw new MyCustomerException(50000, "无法获取到阿里云数据");
        }
        //封装 vo对象
        PlayAuthVo playAuthVo = new PlayAuthVo(image, playId, playAuth);
        return Result.ok(playAuthVo);
    }

}